﻿using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class CarAuditService
    {
        private readonly CarAuditDAO dal = new CarAuditDAO();

        #region  BasicMethod
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CarAudit model)
        {
            var tag = 0;
            if (model.Id == 0)
            {
                tag = dal.Add(model);
            }
            else
            {
                tag = model.Id;
                dal.Update(model);
            }
            return tag;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CarAudit model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CarAudit GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 获取事故申请列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <returns></returns>
        public CommModel<CarAudit> GetList(int orgz, int pageSize, int pageIndex, string start, string end, string carNo)
        {
            string where = string.Empty;
            if (orgz > 0)
            {
                where = "AND OrgzNo=" + orgz;
            }
            if (!string.IsNullOrEmpty(start))
            {
                where += " AND AuditDate>='" + start + "' AND AuditDate<='" + end + "'";
            }
            if (!string.IsNullOrEmpty(carNo))
            {
                where += " AND CarNo Like '%" + carNo + "'";
            }
            Hashtable param = new Hashtable();
            param.Add("A1", pageIndex);
            param.Add("A2", pageSize);
            param.Add("A3", where);
            param.Add("A4", "Vw_CarAudits");
            param.Add("A5", "Id DESC");
            var ht = CommDAO.CommPagination(param);
            DataTable dt = ht["data"] as DataTable;
            List<CarAudit> list = new List<CarAudit>();
            foreach (DataRow item in dt.Rows)
            {
                list.Add(this.dal.DataRowToModel(item));
            }
            CommModel<CarAudit> ent = new CommModel<CarAudit>();
            ent.Data = list;
            ent.PageCount = Convert.ToInt32(ht["PageCount"]);
            ent.RecordCount = Convert.ToInt32(ht["record"]);
            return ent;
        }
        #endregion  ExtensionMethod
    }
}
